package freedoms.yousister.user;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import freedoms.yousister.R;
import freedoms.yousister.common.Contants;
import freedoms.yousister.common.HttpClientUtil;
import freedoms.yousister.response.RegistDeviceResponse;
import freedoms.yousister.response.RegistMobileResponse;

public class RegistMobileActivity extends Activity {
	private Button leftButton;
	private TextView centerTextView;
	private Button rightButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regist_mobile_layout);

		leftButton = (Button) findViewById(R.id.leftButton);
		leftButton.setVisibility(View.INVISIBLE);

		centerTextView = (TextView) findViewById(R.id.centerTextView);
		centerTextView.setText("YOUSISTER");

		rightButton = (Button) findViewById(R.id.rightButton);
		rightButton.setVisibility(View.INVISIBLE);

		findViewById(R.id.okButton).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				registMobileAction();
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
		String device_id = tm.getDeviceId();
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("device_id", device_id));
		System.out.println(device_id);
		HttpClientUtil httpClientUtil = new HttpClientUtil();
		HttpResponse response = httpClientUtil.post(Contants.DEVICE_ISVALID_URL, parameters);
		System.out.println(response.getStatusLine().getStatusCode());
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			try {
				String message = EntityUtils.toString(response.getEntity(), "utf-8");
				System.out.println(message);
				Gson gson = new Gson();
				RegistDeviceResponse registDeviceResponse = gson.fromJson(message, RegistDeviceResponse.class);
				System.out.println("ssssss");
				if (Contants.RES_DEVICE_ID_INVALID.equals(registDeviceResponse.getStatusCode())) {
					Intent intent = new Intent();
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.setClass(RegistMobileActivity.this, UserListActivity.class);
					startActivity(intent);
					RegistMobileActivity.this.finish();
				}
			} catch (ParseException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			Toast.makeText(RegistMobileActivity.this, "mobile访问出错了，请联系管理员", Toast.LENGTH_SHORT).show();
		}
	}

	private void registMobileAction() {
		String mobile = ((EditText) findViewById(R.id.mobile)).getText().toString();
		if (mobile != null && mobile != "") {
			HttpClientUtil httpClientUtil = new HttpClientUtil();
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			parameters.add(new BasicNameValuePair("mobile", mobile));
			HttpResponse response = httpClientUtil.post(Contants.MOBILE_ISVALID_URL, parameters);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				try {
					String message = EntityUtils.toString(response.getEntity(), "utf-8");
					System.out.println(message);
					Gson gson = new Gson();
					RegistMobileResponse registMobileResponse = gson.fromJson(message, RegistMobileResponse.class);
					if (Contants.RES_SUCCESS.equals(registMobileResponse.getStatusCode())) {
						Intent intent = new Intent();
						intent.putExtra("mobile", mobile);
						intent.setClass(RegistMobileActivity.this, RegistUsernameActivity.class);
						startActivity(intent);
					} else {
						Toast.makeText(RegistMobileActivity.this, "手机号码已被使用，请更换后再试", Toast.LENGTH_SHORT).show();
					}
				} catch (ParseException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				Toast.makeText(RegistMobileActivity.this, "访问出错了，请联系管理员", Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(RegistMobileActivity.this, "请填写手机号码", Toast.LENGTH_SHORT).show();
		}
	}

}
